import java.io.*;
import java.util.ArrayList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.*;
import org.jsoup.select.Elements;


public class AllStatesLinksGrabber {
	
	static ArrayList<String> currentStateLinks;

	public static void main(String[] args) {
		String mainLink = "http://www.maxpreps.com/search/states_by_sport.aspx?gendersport=boys,basketball";
		Document doc = null;
		try{
			  doc = Jsoup.connect(mainLink).timeout(0).get();
		} catch (Exception e){
			  System.out.println(mainLink);
		}
		
		int count = 0;
		Elements states = doc.select("li");
		for(Element e: states){
        	if(e.toString().contains("state=")){
        		Element link = e.child(0);
        		System.out.println("" + (++count) + ". " + e.text() + " " + link.attr("href"));
        		String statelink = "http://www.maxpreps.com" + link.attr("href");
        		getStateSchools(statelink, e.text());
        	}
        }
		
	}
	
	public static void getStateSchools(String url, String stateName){
		if(isSmallState(stateName)){
			getSmallStateSchools(url, stateName);
			return;
		}
		currentStateLinks = new ArrayList<String>();
		Document statepage;
		try {
			statepage = Jsoup.connect(url).timeout(0).get();
			Elements divisions = statepage.select("div");
			for(Element e: divisions){
				if(e.toString().contains("basketball teams directory") && e.hasAttr("class")){
					if(e.child(0).attr("href").length() > 0)
						getStatsLinkFromSchoolMain("http://www.maxpreps.com" + e.child(0).attr("href"));
				}
			}
		} catch (IOException e1) {
			e1.printStackTrace();
			System.out.println("Link was bad: " + url);
		}
		writeStateLinksTxt(stateName);
	}
	
	public static boolean isSmallState(String stateName){
		if(stateName.equals("Delaware") || stateName.equals("District of Columbia") || 
		   stateName.equals("Hawaii") || stateName.equals("New Hampshire") || 
		   stateName.equals("Rhode Island") || stateName.equals("Vermont") || stateName.equals("Wyoming"))
			return true;
		return false;
	}
	
	public static void getSmallStateSchools(String url, String stateName){
		currentStateLinks = new ArrayList<String>();
		Document statepage;
		try {
			statepage = Jsoup.connect(url).timeout(0).get();
			Elements divisions = statepage.select("div");
			for(Element e: divisions){
				Elements ultags = e.select("ul");
				for(Element ul: ultags){
					if(ul.toString().contains("/high-schools/")){
						Elements litags = ul.select("li");
						for(Element li: litags){
							if(li.child(0).attr("href").length() > 0){
								String toAdd = "http://www.maxpreps.com" + li.child(0).attr("href");
								String fixed = toAdd.replace("home.htm", "stats.htm");
								if(!currentStateLinks.contains(fixed)){
									currentStateLinks.add(fixed);
								}
							}
						}
					}
				}
			}
		} catch (IOException e1) {
			e1.printStackTrace();
			System.out.println("Link was bad: " + url);
		}
		writeStateLinksTxt(stateName);
	}
	
	public static void getStatsLinkFromSchoolMain(String url){
		Document schoolpage;
		try {
			schoolpage = Jsoup.connect(url).timeout(0).get();
			Elements varsitylinks = schoolpage.select("li");
			for(Element e: varsitylinks){
				if(e.text().equals("Varsity")){
					String statlink = "http://www.maxpreps.com" + e.child(0).attr("href");
					currentStateLinks.add(statlink.replace("home.htm", "stats.htm"));
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void writeStateLinksTxt(String stateName){
		try {
			boolean success = (new File("C:\\Users\\Dallin\\Downloads\\" + stateName + " Stats\\")).mkdirs();
			String filename = "C:\\Users\\Dallin\\Downloads\\" + stateName + "-links.txt";
			
			FileWriter outFile = new FileWriter(filename);
			PrintWriter out = new PrintWriter(outFile);

			// Write text to file
			for(String link: currentStateLinks){
				out.println(link);
			}
			out.close();
			
			MaxStatsParser allParser = new MaxStatsParser();
			String[] args = new String[2];
			args[0] = filename;
			args[1] = stateName;
			allParser.main(args);
		} catch (Exception e){
			e.printStackTrace();
		}
	}

}
